package com.example.admin.controller.question;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.question.entity.QuestionSet;
import com.example.question.entity.QuestionSetQueryDTO;
import com.example.question.entity.Result;
import com.example.question.service.QuestionSetService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 题目集控制器
 * @author 颜殿
 */
@RestController
@RequestMapping("/api/questionSet")
public class QuestionSetController {

    @Autowired
    private QuestionSetService questionSetService;

    /**
     * 添加题目集
     * @param questionSet 题目集实体
     * @return 操作结果
     */
    @PostMapping
    public Result<Boolean> addQuestionSet(@RequestBody QuestionSet questionSet) {
        return Result.success(questionSetService.addQuestionSet(questionSet));
    }

    /**
     * 删除题目集
     * @param id 要删除的题目集ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteQuestionSet(@PathVariable Integer id) {
        return Result.success(questionSetService.deleteQuestionSet(id));
    }

    /**
     * 修改题目集
     * @param questionSet 题目集实体
     * @return 操作结果
     */
    @PutMapping
    public Result<Boolean> updateQuestionSet(@RequestBody QuestionSet questionSet) {
        return Result.success(questionSetService.updateQuestionSet(questionSet));
    }

    /**
     * 根据职位ID分页查询题目集
     * @param pageNum 页码
     * @param pageSize 条数
     * @param positionId 职位ID
     * @return 返回数据
     */
    @GetMapping("/page/position/{positionId}")
    public Result<Page<QuestionSet>> pageByPosition(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @PathVariable Integer positionId) {
        return Result.success(questionSetService.pageByPosition(pageNum, pageSize, positionId));
    }

    /**
     * 根据职位级别分页查询题目集
     * @param pageNum 页码
     * @param pageSize 条数
     * @param level 职位级别
     * @return 返回数据
     */
    @GetMapping("/page/positionLevel")
    public Result<Page<QuestionSet>> pageByPositionLevel(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @RequestBody String level) {
        return Result.success(questionSetService.pageByPositionLevel(pageNum, pageSize, level));
    }

    /**
     * 根据状态分页查询题目集
     * @param pageNum 页码
     * @param pageSize 条数
     * @param status 状态
     * @return 返回数据
     */
    @GetMapping("/page/status/{status}")
    public Result<Page<QuestionSet>> pageByStatus(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @PathVariable Integer status) {
        return Result.success(questionSetService.pageByStatus(pageNum, pageSize, status));
    }

    /**
     * 多条件分页查询题目集
     */
    @PostMapping("/page/condition")
    public Result<Page<QuestionSet>> pageByCondition(@RequestBody QuestionSetQueryDTO queryDTO) {
        return Result.success(questionSetService.pageByCondition(
                queryDTO.getPageNum(),
                queryDTO.getPageSize(),
                queryDTO.getPositionsId(),
                queryDTO.getLevel(),
                queryDTO.getStatus()
        ));
    }

    @GetMapping("/position/{id}")
    public ResponseEntity<Map<String, Object>> getPositionByQuestionSetId(@PathVariable Long id) {
        Object result = questionSetService.getPositionByQuestionSetId(id);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("msg", "查询成功");
        response.put("data", result);

        return ResponseEntity.ok(response);
    }
} 